<?php
class admin_stats extends CI_Controller {
	protected $_page_name = "admin_stats";
	protected $_page_id = 'ul_postcontainer';
	protected $data; //data for this page.
	protected $submitdata; //data submit.
	protected $_base_url;
	
	function  __construct()  {
		$this->_initdata();
	}
	
	//Controller: default
	function index() {
		$this->data['sStats'] = $this->get_memcached_info();
		$this->data['page_name'] = "admin_stats_memcached";		
		$this->data['page_title'] = "Memcached";
		$this->showadmin($this->data);		
	}		
	
	//Controller: memcache_set
	function memcache_set()
	{
		if (!empty($_POST['isPost']) && ($_POST['isPost']==1)) {
			if (MEMCACHED_ENABLED=="true")
			{
				$memcache = $this->mem_connect(MEMCACHED_IP, MEMCACHED_PORT);
				$memcache->set($_POST['txt_mem_key'], $_POST['txt_mem_key'], false, 10) or
				die ("Failed to save data at the server");
				$memcache->close();
			}
			$this->load->helper('url');
			echo "<script>window.location='".$this->data['base_url'].'admin_stats/'."'</script>";				
		}
	}
	
	//local func: 
	function mem_connect($mem_server_ip, $mem_server_port)
	{
		$memcache = new Memcache;
		$memcache->connect(MEMCACHED_IP, MEMCACHED_PORT) or die ("<div style='color:red;font-family:tahoma;font-size:11px;'>Could not connect to memcached server: <b>".MEMCACHED_IP.":".MEMCACHED_PORT."</b></div>");
		return $memcache;
	}
	
	//local func: 
	function get_memcached_info()
	{
		$str = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Memcached is not enabled in ".WEBSITE_NAME."'s config";
		if (MEMCACHED_ENABLED=="true")
		{		
			$memcache = $this->mem_connect(MEMCACHED_IP, MEMCACHED_PORT);
			$list = array();
			$allSlabs = $memcache->getExtendedStats('slabs');
			//$this->Adminmodel->do_dump($allSlabs);
			$items = $memcache->getExtendedStats('items');
			//$this->Adminmodel->do_dump($items);
			//$this->Adminmodel->do_dump($memcache->getStats());
			/*
			$ver = $memcache->getVersion();
			echo "Server's version: ". $ver ."<br/>\n";
			$oTmp = new stdClass;
			$oTmp->str_attr = 'testing memcached';
			$oTmp->int_attr = 12345;
			$memcache->set('key', $oTmp, false, 10) or
			die ("Failed to save data at the server");
			echo "Store data in the cache (data will expire in 10 seconds)
			<br/>\n";
			$res = $memcache->get('key');
			echo "Data from the cache:<br/>\n";
			var_dump($res);	
			$this->Adminmodel->do_dump($memcache->getStats());	
			*/
			$str = $this->Adminmodel->_get_memcached_info($memcache->getStats());		
			$memcache->close();
		}
		return $str;
	}

	//local func: init data
	function _initdata()
	{
		parent::__construct();
		session_start();
		$this->load->model('Adminmodel');
		$this->load->helper('url');
		$this->_check_admin_login();
		
		//init data (this page, header admin)
		$this->_base_url = $this->config->item('base_url');
		$this->data['base_url'] = $this->config->item('base_url');
		$this->data['admintitle_news'] = ADMINTITLE_NEWS;
                $this->data['admintitle_news_auto'] = ADMINTITLE_NEWS_AUTO;
                $this->data['admintitle_video'] = ADMINTITLE_VIDEO;
                $this->data['admintitle_customs'] = ADMINTITLE_CUSTOMS;
		$this->data['admintitle_adv'] = ADMINTITLE_ADV;
		$this->data['admintitle_faq'] = ADMINTITLE_FAQ;
		$this->data['admintitle_contact'] = ADMINTITLE_CONTACT;
		$this->data['admintitle_common'] = ADMINTITLE_COMMON;
		$this->data['admintitle_stats'] = ADMINTITLE_STATS;
		$this->data['admintitle_beuser'] = ADMINTITLE_BEUSER;
		$this->data['website_name'] = WEBSITE_NAME;		
		$this->data['link_page_name'] = $this->_page_name;
		$this->data['is_addnew'] = ""; //show link add / list
		$this->data['page_id'] = $this->_page_id;	
	}			
	
	//local func: check admin login
	function _check_admin_login()
	{
		if (($this->Adminmodel->_check_login(SESSION_ADMINUSERNAME) == false) ||
				(time() - $this->Adminmodel->_get_session(SESSION_ADMINTIMEOUT)) > SESSION_ADMINTIMEOUTVALUE
			 )
		{
			$base_url=$this->config->item('base_url');
			redirect($base_url.'admin_login', 'location', 301);
		}		
		else
			$this->Adminmodel->_set_session(SESSION_ADMINTIMEOUT,time());
	}		
	
}
?>